package com.course.business.controller.web.discusspost;

import com.course.business.controller.web.discusspost.event.EventProducer;
import com.course.server.domain.Member;
import com.course.server.domain.discusspost.DiscussPost;
import com.course.server.domain.discusspost.Event;
import com.course.server.dto.PageDto;
import com.course.server.dto.ResponseDto;
import com.course.server.dto.discusspost.DiscussPostQueryById_Z1;
import com.course.server.service.discusspost.DiscussPostService;
import com.course.server.util.CommunityConstant;
import com.course.server.util.HostHolder;
import com.course.server.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/web/discuss")
public class DiscussPostController implements CommunityConstant {

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private EventProducer eventProducer;

    @Resource(name="myRedisTemplate")
    private RedisTemplate redisTemplate;

    @RequestMapping(path = "/add", method = RequestMethod.POST)
    public ResponseDto addDiscussPost(@RequestBody Map<String,String> map) {
        String title=map.get("title");
        String content=map.get("content");
        String courseId=map.get("courseId");

        Member user = hostHolder.getMember();
        if (user == null) {
            return new ResponseDto(false,"你还没有登录哦!");
        }

        DiscussPost post = new DiscussPost();
        post.setUserId(user.getId());
        post.setTitle(title);
        post.setContent(content);
        post.setCreateTime(new Date());
        post.setCourseId(courseId);
        discussPostService.addDiscussPost(post);

        // 触发发帖事件
        Event event = new Event()
                .setTopic(TOPIC_PUBLISH)
                .setUserId(user.getId())
                .setEntityType(ENTITY_TYPE_POST)
                .setEntityId(post.getId());
        eventProducer.fireEvent(event);

        // 计算帖子分数
        String redisKey = RedisKeyUtil.getPostScoreKey();
        redisTemplate.opsForSet().add(redisKey, post.getId());

        return new ResponseDto();
    }

    @RequestMapping(path = "/detail/{discussPostId}")
    public ResponseDto getDiscussPost(@PathVariable("discussPostId") String discussPostId,@RequestBody PageDto pageDto) {

        ResponseDto responseDto=new ResponseDto();
        DiscussPostQueryById_Z1 discussPostQueryById_z1 = discussPostService.findDiscussPostById(discussPostId, pageDto);
        responseDto.setContent(discussPostQueryById_z1);
        return responseDto;
    }

    // 置顶
    @RequestMapping(path = "/top", method = RequestMethod.POST)
    public ResponseDto setTop(@RequestBody Map<String,String> map) {
        String id = map.get("id");

        // 触发发帖事件
        Event event = new Event()
                .setTopic(TOPIC_PUBLISH)
                .setUserId(hostHolder.getMember().getId())
                .setEntityType(ENTITY_TYPE_POST)
                .setEntityId(id);
        eventProducer.fireEvent(event);

        return new ResponseDto();
    }

    // 加精
    @RequestMapping(path = "/wonderful", method = RequestMethod.POST)
    public ResponseDto setWonderful(String id) {
        discussPostService.updateStatus(id, 1);

        // 触发发帖事件
        Event event = new Event()
                .setTopic(TOPIC_PUBLISH)
                .setUserId(hostHolder.getMember().getId())
                .setEntityType(ENTITY_TYPE_POST)
                .setEntityId(id);
        eventProducer.fireEvent(event);

        // 计算帖子分数
        String redisKey = RedisKeyUtil.getPostScoreKey();
        redisTemplate.opsForSet().add(redisKey, id);

        return new ResponseDto();
    }

    // 删除
    @RequestMapping(path = "/delete", method = RequestMethod.POST)
    public ResponseDto setDelete(String id) {
        discussPostService.updateStatus(id, 2);

        // 触发删帖事件
        Event event = new Event()
                .setTopic(TOPIC_DELETE)
                .setUserId(hostHolder.getMember().getId())
                .setEntityType(ENTITY_TYPE_POST)
                .setEntityId(id);
        eventProducer.fireEvent(event);

        return new ResponseDto();
    }

}
